//package com.hfzz.common.core.handler;
//
//import com.hfzz.common.core.entity.Result;
//import com.hfzz.common.core.exception.BaseException;
//import com.hfzz.common.core.exception.SystemErrorType;
//import jakarta.servlet.http.HttpServletRequest;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.http.converter.HttpMessageNotReadableException;
//import org.springframework.stereotype.Component;
//import org.springframework.validation.BindException;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.MethodArgumentNotValidException;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.multipart.MaxUploadSizeExceededException;
//
//import javax.validation.ConstraintViolationException;
//
///**
// * @author jiangzhao
// */
//@ControllerAdvice
//@Component
//@Slf4j
//public class GlobalExceptionHandler {
//
//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public Result defaultExceptionHandler(Exception e, HttpServletRequest request) {
//        if (e instanceof BaseException) {
//            BaseException baseException = (BaseException) e;
//            return Result.fail(baseException);
//        }
//        if (e instanceof MethodArgumentNotValidException) {
//            //@Valid 校验异常捕获 对POST请求的RequestBody做校验
//            BindingResult result = ((MethodArgumentNotValidException) e).getBindingResult();
//            String msg = result.getFieldErrors().stream().map(fe -> fe.getField() + fe.getDefaultMessage())
//                    .reduce((m1, m2) -> m1 + "; " + m2)
//                    .orElse(SystemErrorType.ARGUMENT_NOT_VALID.getMsg());
//
//            return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID.getCode(), msg);
//        } else if (e instanceof BindException) {
//            //@Valid 校验异常捕获 对GET请求做参数校验
//            BindingResult result = ((BindException) e).getBindingResult();
//            String msg = result.getFieldErrors().stream().map(fe -> fe.getField() + fe.getDefaultMessage())
//                    .reduce((m1, m2) -> m1 + "; " + m2)
//                    .orElse(SystemErrorType.ARGUMENT_NOT_VALID.getMsg());
//
//            return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID.getCode(), msg);
//        } else if (e instanceof ConstraintViolationException) {
//            //@Validated 校验异常捕获 对PathVariable和RequestParam参数校验，Validated在类上
//
//
//            ConstraintViolationException exception = (ConstraintViolationException) e;
//            String msg = exception.getConstraintViolations().stream().map(violation ->
//                    violation.getPropertyPath().toString() + ":" + violation.getMessage() + ","
//            ).reduce("", (s1, s2) -> s1 + s2);
//
//            return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID.getCode(), msg);
//        } else if (e instanceof HttpMessageNotReadableException) {
//            return Result.fail(SystemErrorType.ARGUMENT_NOT_VALID.getCode(), SystemErrorType.ARGUMENT_NOT_VALID.getMsg());
//        } else if (e instanceof MaxUploadSizeExceededException) {
//            return Result.fail(SystemErrorType.FILE_SIZE_EXCEEDED.getCode(), SystemErrorType.FILE_SIZE_EXCEEDED.getMsg());
//        }
//        log.error("GlobalExceptionHandler defaultExceptionHandler. req url {} , e ==>>", request.getRequestURI(), e);
//        //未知错误
//        return Result.fail();
//    }
//}